package com.schoolET.controller.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.schoolET.entity.Goods;
import com.schoolET.entity.vo.GoodCdVO;
import com.schoolET.entity.vo.InquireGoodsVO;
import com.schoolET.error.costom.NoGoodsException;
import com.schoolET.error.costom.PermissionsException;
import com.schoolET.result.MyPage;
import com.schoolET.result.Result;
import com.schoolET.service.admin.IGoodsManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author: lin
 * @className: GoodsManageController
 * @createTime: 2024-05-31  21:06
 * @description: 商品管理
 */
@Api(tags = "商品管理")
@RestController
@RequestMapping("/schoolET")
public class GoodsManageController {

    @Resource
    private IGoodsManageService goodsManageService;

    @ApiOperationSupport(author = "lin")
    @ApiOperation(value = "查询商品")
    @PostMapping("/goodsList")
    @SaCheckLogin
    @SaCheckRole(mode = SaMode.OR, value = {"admin", "region-admin"})
    public Result inquireGoods(@RequestBody InquireGoodsVO inquireGoodsVO) {
        if (StpUtil.hasPermission("2") && inquireGoodsVO.getPosition() != null) {
            throw new PermissionsException();
        }
        return Result.success(MyPage.page(goodsManageService.inquireGoods(inquireGoodsVO))).msg("查询成功");
    }

    @ApiOperationSupport(author = "lin")
    @ApiOperation(value = "修改商品")
    @PostMapping("/goodsCg")
    @SaCheckLogin
    @SaCheckRole(mode = SaMode.OR, value = {"admin", "region-admin"})
    public Result goodsCg(GoodCdVO goodCdVO, MultipartFile file) throws IOException {
        Goods goods = new Goods();
        BeanUtil.copyProperties(goodCdVO, goods);
        goodsManageService.goodsCg(goods,file);
        return Result.success().msg("修改成功");
    }

    @ApiOperationSupport(author = "lin")
    @ApiOperation(value = "删除商品")
    @PostMapping("/goodsDel/{id}")
    @SaCheckLogin
    @SaCheckRole(mode = SaMode.OR, value = {"admin", "region-admin"})
    public Result goodsDel(@PathVariable Integer id) {
        if (goodsManageService.getById(id) == null) {
            throw new NoGoodsException();
        }
        goodsManageService.goodsDel(id);
        return Result.success().msg("删除成功");
    }
}
